let mongoose = require('mongoose'),
    async = require('async'),
    memberUsers = mongoose.model('member_users');

module.exports = (req, res) => {
    let params = req.body,
        me = this;

    if(!params._id){
        res.end({
            result: 0,
            message: '_id不能为空'
        });
        return false;
    }
    if(!params.operatorId){
        res.end({
            result: 0,
            message: '操作者ID不能为空'
        })
        return false;
    }

    async.series([
        // 查询操作者
        callback => {
            memberUsers.find({
                _id: params.operatorId
            }, (err, data) => {
                if(err){
                    res.json({
                        result: 0,
                        message: '查询operatorId出错'
                    })
                }else{
                    if(!data.length){
                        res.json({
                            result: 0,
                            message: 'operatorId不存在'
                        })
                    }else{
                        callback();
                    }
                }
            })
        },
        // 查询目标用户
        callback => {
            memberUsers.find({
                _id: {
                    '$in': params._id
                }
            }, (err, data) => {
                if(err){
                    res.json({
                        result: 0,
                        message: '查询目标用户出错'
                    })
                }else{
                    me.userList = data;
                    callback();
                }
            })
        },  
        // 删除目标用户
        callback => {
            memberUsers.remove({
                _id: {
                    '$in': params._id
                }
            }, err => {
                if(err){
                    res.json({
                        result: 0,
                        message: '删除出错'
                    })
                }else{
                    res.json({
                        result: 1,
                        message: '删除成功。'
                    })
                }
            })
        }
    ])
}